import { createContext, useEffect, useState, useContext } from "react";
import { LoginContext } from "./login-provider";
import { getUserInfo } from "@/api/login";

interface UserContextType {
  member: any;
  getUser: () => void;
  setMember: (member: any) => void;
}

export const UserContext = createContext<UserContextType>({
  member: null,
  getUser: () => {},
  setMember: () => {},
});

const UserProvider = ({ children }: { children: React.ReactNode }) => {
  const { userInfo } = useContext(LoginContext);
  const [member, setMember] = useState(null);
  const getUser = async () => {
    if (userInfo) {
      const res = await getUserInfo();
      setMember(res.data);
    }
  };
  useEffect(() => {
    getUser();
  }, [userInfo]);
  return (
    <UserContext.Provider
      value={{
        member,
        setMember,
        getUser,
      }}
    >
      {children}
    </UserContext.Provider>
  );
};

export default UserProvider;
